package com.djzhou.gmms.menu.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import jodd.util.StringUtil;

import org.apache.struts2.ServletActionContext;

import com.djzhou.gmms.base.action.BaseAction;
import com.djzhou.gmms.model.authority.TbAuthority;
import com.djzhou.gmms.model.company.TbCompany;
import com.djzhou.gmms.model.department.TbDepartment;
import com.djzhou.gmms.model.menu.TbMenu;
import com.djzhou.gmms.model.menu.TbMenuAuthority;
import com.djzhou.gmms.model.menu.TbUserMenuConfig;
import com.opensymphony.xwork2.ModelDriven;

public class MenuAction extends BaseAction<TbMenu> implements ModelDriven<TbMenu> {

	private static final long serialVersionUID = 1L;
	private TbMenu menu;
	private Integer menuId;
	private Integer parentMenuId;
	private List<TbMenu> list;
	private String menuName;
	private List<TreeMenuPojo> treeStore;
	
	private List<TbMenu> menuList;
	private List<TbDepartment> departmentList;
	private List<TbAuthority> authorityList;
	private List<TbMenuAuthority> menuAuthorityList;
	private List<TbCompany> companyList; 
	
	private String authorityString;
	private Integer departmentId;
	private Boolean openFlag;
	private String sortMenuIds;
	private Integer oldParentMenuId;
	
	private List<TbMenu> appsList;
	private List<TbUserMenuConfig> oftenAppsList;
	
	public String loadMenu(){
		menuList=menuService.queryMenuListByParentId(parentMenuId);
		return SUCCESS;
	}
	
	public String queryApps(){
		appsList=menuService.queryApps(queryCompany().getCompanyId(), queryDepartment().getDepartmentId(), queryRoleIds4Java());
		oftenAppsList=menuService.queryOftenApps(queryUser().getUserId(),queryCompany().getCompanyId(), queryDepartment().getDepartmentId(), queryRoleIds4Java());
		return SUCCESS;
	}
	
	public String resetOrder(){
		menuService.updateResetAllMenuLevelCode();
		logService.addLog("菜单管理", queryUser(), "变更序列化", 0, "TbMenu", queryCompany(),super.queryDepartment());
		return SUCCESS;
	}
	
	public String sortMenu(){
		menuService.updateSortMenu(sortMenuIds,parentMenuId);
		logService.addLog("菜单管理", queryUser(), "变更菜单顺序", 0, "TbMenu", queryCompany(),super.queryDepartment());
		return SUCCESS;
	}
	
	
	/**
	 * 获取左侧树形菜单数据源
	 * @return
	 */
	public String queryTreeStore(){
		List<TbMenu> parentMenuList=menuService.queryMenuListByParentId(0,queryCompany().getCompanyId(),queryDepartment().getDepartmentId(),queryRoleIds4Java());
		List<TbMenu> childMenuList=new ArrayList<TbMenu>();
		TbMenu parentMenu=new TbMenu();
		TbMenu childMenu=new TbMenu();
		String projectPath=ServletActionContext.getRequest().getContextPath();
		treeStore=new ArrayList<TreeMenuPojo>();
		for(Iterator<TbMenu> i = parentMenuList.iterator(); i.hasNext();)   {
			parentMenu=i.next();
			TreeMenuPojo parentPojo=new TreeMenuPojo();
			parentPojo.setKeyId(parentMenu.getMenuId());
			parentPojo.setIcon(parentMenu.getMenuIcon());
			parentPojo.setName(parentMenu.getMenuName());
			if(parentMenu.getParentMenuId()==0)parentPojo.setOpen("true");
			else parentPojo.setOpen("false");
			parentPojo.setParentId(0);
			parentPojo.setTarget("main");
			if(parentMenu.getMenuUrl()!=null && !parentMenu.getMenuUrl().equals(""))parentPojo.setUrl(projectPath+parentMenu.getMenuUrl()+"?currentMenuId="+parentMenu.getMenuId());
			parentPojo.setTitle(parentMenu.getMenuDesc());
			treeStore.add(parentPojo);
			childMenuList=menuService.queryMenuListByParentId(parentMenu.getMenuId(),queryCompany().getCompanyId(),queryDepartment().getDepartmentId(),queryRoleIds4Java());
			for(Iterator<TbMenu> j = childMenuList.iterator(); j.hasNext();)   {
				childMenu=j.next();
				TreeMenuPojo childPojo=new TreeMenuPojo();
				childPojo.setKeyId(childMenu.getMenuId());
				childPojo.setIcon(childMenu.getMenuIcon());
				childPojo.setName(childMenu.getMenuName());
				childPojo.setParentId(childMenu.getParentMenuId());
				childPojo.setTarget("main");
				if(childMenu.getMenuUrl()!=null && !childMenu.getMenuUrl().equals(""))childPojo.setUrl(projectPath+childMenu.getMenuUrl()+"?currentMenuId="+childMenu.getMenuId());
				childPojo.setTitle(childMenu.getMenuDesc());
				treeStore.add(childPojo);
			}
		 }
		
		return SUCCESS;
	}
	
	@Override
	public String delete() {
		menuService.delete(menuId);
		msg="删除成功";
		setCurrentPage(currentPage);
		logService.addLog("菜单管理",queryUser(),"删除",menuId,"TbMenu",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String modify() {
		TbDepartment department=departmentService.queryById(departmentId);
		menu.setLastModifyTime(new Date());
		menu.setUser(queryUser());
		menu.setDepartment(department);
		menu.setCompany(department.getCompany());
		
		menuService.update(menu,authorityString);
		msg="修改成功";
		setCurrentPage(currentPage);
		logService.addLog("菜单管理",queryUser(),"编辑",menuId,"TbMenu",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String queryList() {
		if(loadData==null)
			return SUCCESS;
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		hashMap.put("companyId", queryCompany().getCompanyId());
		hashMap.put("departmentId", queryDepartment().getDepartmentId());
		if(StringUtil.isNotEmpty(menuName))hashMap.put("menuName", menuName);
		pageBean=menuService.queryPageBean(hashMap, pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean=menuService.queryPageBean(hashMap, pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbMenu>();
		}
		totalPage=pageBean.getTotalPage();
		return SUCCESS;
	}

	@Override
	public String save() {
		if(menu!=null){
			TbDepartment department=departmentService.queryById(departmentId);
			Date date=new Date();
			menu.setCreateTime(date);
			menu.setLastModifyTime(date);
			menu.setDeleteFlag(0);
			menu.setDepartment(department);
			menu.setCompany(department.getCompany());
			menu.setUser(queryUser());
			menuService.add(menu,authorityString);
		}
		setCurrentPage(currentPage);
		logService.addLog("菜单管理",queryUser(),"添加",menuId,"TbMenu",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String toAdd() {
		menuList=menuService.queryListForAction(queryCompany().getCompanyId(), 1, 0);
		//departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
		//companyList=companyService.queryList(1);//查询除默认公司外的公司
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		authorityList=authorityService.queryList(hashMap);
		return SUCCESS;
	}

	@Override
	public String toEdit() {
		
		setCurrentPage(currentPage);
		
		menu=menuService.queryById(menuId);
		
		menuList=menuService.queryListForAction(queryCompany().getCompanyId(), menu.getDepartment().getDepartmentId(), 0);
		
		//departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
		//companyList=companyService.queryList(1);//查询除默认公司外的公司
		
		menuAuthorityList=menuAuthorityService.queryList(menuId);
		
		authorityString=new String();
		StringBuffer sb=new StringBuffer();
		sb.append(",");
		for(TbMenuAuthority ma:menuAuthorityList){
			sb.append(ma.getAuthority().getAuthorityId()).append(",");
		}
		authorityString=sb.toString();
		
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		authorityList=authorityService.queryList(hashMap);
		return SUCCESS;
	}
	
	public TbMenu getModel() {
		return menu;
	}

	public TbMenu getMenu() {
		return menu;
	}

	public void setMenu(TbMenu menu) {
		this.menu = menu;
	}

	public Integer getMenuId() {
		return menuId;
	}

	public void setMenuId(Integer menuId) {
		this.menuId = menuId;
	}

	public List<TbMenu> getList() {
		return list;
	}

	public void setList(List<TbMenu> list) {
		this.list = list;
	}

	public Integer getParentMenuId() {
		return parentMenuId;
	}

	public void setParentMenuId(Integer parentMenuId) {
		this.parentMenuId = parentMenuId;
	}

	public List<TreeMenuPojo> getTreeStore() {
		return treeStore;
	}

	public void setTreeStore(List<TreeMenuPojo> treeStore) {
		this.treeStore = treeStore;
	}

	public List<TbMenu> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<TbMenu> menuList) {
		this.menuList = menuList;
	}

	public List<TbDepartment> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<TbDepartment> departmentList) {
		this.departmentList = departmentList;
	}

	public Integer getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Integer departmentId) {
		this.departmentId = departmentId;
	}

	public Boolean getOpenFlag() {
		return openFlag;
	}

	public void setOpenFlag(Boolean openFlag) {
		this.openFlag = openFlag;
	}

	public List<TbAuthority> getAuthorityList() {
		return authorityList;
	}

	public void setAuthorityList(List<TbAuthority> authorityList) {
		this.authorityList = authorityList;
	}

	public String getAuthorityString() {
		return authorityString;
	}

	public void setAuthorityString(String authorityString) {
		this.authorityString = authorityString;
	}

	public List<TbMenuAuthority> getMenuAuthorityList() {
		return menuAuthorityList;
	}

	public void setMenuAuthorityList(List<TbMenuAuthority> menuAuthorityList) {
		this.menuAuthorityList = menuAuthorityList;
	}

	public String getSortMenuIds() {
		return sortMenuIds;
	}

	public void setSortMenuIds(String sortMenuIds) {
		this.sortMenuIds = sortMenuIds;
	}

	public List<TbCompany> getCompanyList() {
		return companyList;
	}

	public void setCompanyList(List<TbCompany> companyList) {
		this.companyList = companyList;
	}

	public Integer getOldParentMenuId() {
		return oldParentMenuId;
	}

	public void setOldParentMenuId(Integer oldParentMenuId) {
		this.oldParentMenuId = oldParentMenuId;
	}

	public List<TbMenu> getAppsList() {
		return appsList;
	}

	public void setAppsList(List<TbMenu> appsList) {
		this.appsList = appsList;
	}

	public List<TbUserMenuConfig> getOftenAppsList() {
		return oftenAppsList;
	}

	public void setOftenAppsList(List<TbUserMenuConfig> oftenAppsList) {
		this.oftenAppsList = oftenAppsList;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

}
